package lk.icta.core.service.impl;

import lk.icta.common.ELandRuntimeException;
import lk.icta.common.ErrorCodes;
//import lk.icta.common.util.MailUtil;
import lk.icta.core.dao.RegisteredPostDAO;
import lk.icta.core.entity.RegisteredPost;
import lk.icta.core.service.RegisteredPostManagementService;
import lk.icta.user.common.Permission;
import lk.icta.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Kusal Samarakoon
 */

public class RegisteredPostManagementServiceImpl implements RegisteredPostManagementService {

    private static final Logger logger = LoggerFactory.getLogger(RegisteredPostManagementServiceImpl.class);
    private final RegisteredPostDAO registeredPostDAO;

    public RegisteredPostManagementServiceImpl(RegisteredPostDAO registeredPostDAO) {
        this.registeredPostDAO = registeredPostDAO;

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addRegisteredPost(RegisteredPost registeredPost, User user) {
        logger.debug("Adding a RegisteredPost {}", registeredPost.getId());
        registeredPostDAO.addRegisteredPost(registeredPost, user);
        if (!(user.isAuthorized(Permission.MANAGE_INITIAL_LAND_APPLICATION) ||
                user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION))) {
            throw new ELandRuntimeException(user.getUsername() +
                    " doesn't have permission to add a RegisteredPost", ErrorCodes.UNAUTHORIZED_ACCESS);
        } else {
            registeredPostDAO.addRegisteredPost(registeredPost, user);
        }
    }

}
